//
//  CZNewFeatureViewController.swift
//  weibo
//
//  Created by Mac on 16/5/17.
//  Copyright © 2016年 fengzhixiang. All rights reserved.
//

import UIKit

//给cell赋值重用Id 
private let reuseIdentifier = "cell"
//给新特性页面设置总页数
private let ItemCount = 4

class CZNewFeatureViewController: UICollectionViewController {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 属性
    ///layout
    private let layout = UICollectionViewFlowLayout()

    // 创建这个类的时候调用该方法
    init(){
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置重用标识的cell
        self.collectionView?.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        //设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size// 让cell和屏幕一样大
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal// 水平滚动
        //去除间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0

        collectionView?.bounces = false //取消弹簧效果
        collectionView?.pagingEnabled = true

    }


}




//拓展数据源方法
extension CZNewFeatureViewController {
    // 某一组显示几个cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
    // 每个cell张什么样
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // dequeueReusableCellWithReuseIdentifier: 取cell会先去缓存池中取cell,缓存池中取不到会使用注册的cell来创建
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
//        cell.backgroundColor = UIColor.greenColor()
        // 设置cell显示的index
        cell.index = indexPath.item

        return cell
    }

    /// scrollView停止滚动
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //获取collectionView的偏移值,计算出当前停下来的是哪一页,collectionView本身自带contentOffset属性
        let index = Int(scrollView.contentOffset.x / scrollView.frame.width)
         CZPrint(items: "scrollView停止滚动, 在第 \(index) 页")

        //判断是否是最后一页
        if index == ItemCount - 1{
            // 获取cell,千万不要自己去创建cell,应该拿到正在显示的cell
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! CZNewFeatureCell
            cell.startAnimation()

        }
    }

}








